package com.example.demo.controller;

import com.example.demo.auth.AuthTokenAnno;
import com.example.demo.config.AuthenticationInterceptor;
import com.example.demo.dao.mapper.TestBeanMapper;
import com.example.demo.dao.model.SysToken;
import com.example.demo.dao.model.TestBean;
import com.example.demo.dao.model.TestBeanCriteria;
import com.example.demo.service.ITestService;
import com.example.demo.service.ITokenService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author Rocket
 * @version 10.0.1
 * @date 2021-09-05 9:21 AM
 */
@RestController
@RequestMapping("demo")
public class DemoController {

    @Autowired
    private TestBeanMapper mapper;
    @Autowired
    private ITestService testService;

    @Autowired
    private ITokenService tokenService;

    @RequestMapping("/hello")
    @AuthTokenAnno(required = true, role = "Manager")
    public String sayHello() {

        return "Hi, there, I am from controller";
    }
    @RequestMapping("/date")
    public Date sayDate() {

        return new Date();
    }

    @GetMapping("/test/list")
    public List<TestBean> getTestBeans(Integer pageIndex, Integer pageSize) {
        TestBeanCriteria criteria = new TestBeanCriteria();
        //criteria.createCriteria().andNameEqualTo("jinxin");
        PageHelper.startPage(pageIndex, pageSize);
        return mapper.selectByCriteria(criteria);
    }

    @GetMapping("/test/advanceSearch")
    public List<TestBean> advancedSearch(String keyword, Integer pageIndex, Integer pageSize) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        if(null == pageIndex || pageSize == null) {
            throw new RuntimeException("parameter is incorrect.");
        }
        return this.testService.advanceSearch(keyword, pageIndex, pageSize);
    }

    @GetMapping("/test/{id}")
    public TestBean getTestBean(@PathVariable("id") Integer id) {
        return this.mapper.selectByPrimaryKey(id);
    }


    @PostMapping("/test/new")
    public TestBean createTestBean(@RequestBody TestBean testBean) {
        int count = this.mapper.insertSelective(testBean);
        return testBean;
    }

    @PutMapping("/test/{id}/edit")
    public TestBean updateTestBean(@RequestBody TestBean testBean, @PathVariable("id") Integer id) {

        int count = this.mapper.updateByPrimaryKeySelective(testBean);
        return testBean;
    }

    @DeleteMapping("/test/{id}/delete")
    public ResponseEntity deleteTestBean(@PathVariable("id") Integer id) {

        int count = this.mapper.deleteByPrimaryKey(id);
        if(count >0) {
            return ResponseEntity.ok(String.format("Delete %d completed.", id));
        }
        return ResponseEntity.status(500).body(String.format("Failed to delete %d.", id));
    }

    @GetMapping("/test/logout/{tokenId}")
    public boolean logout(HttpServletRequest request, @PathVariable("tokenId") String tokenId) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(tokenId)) {
            throw new RuntimeException("Token id is empty.");
        }
        if(token != null) {
            SysToken sysToken = new SysToken();
            sysToken.setId(Integer.parseInt(tokenId));
            sysToken.setBlackFlag(1);
            this.tokenService.updateToken(sysToken);
        }
        // revoke resources....

        return true;
    }

}
